﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TI;
using System.Xml;
using System.Xml.Serialization;
using TiPort.TiEntityObjects;
using MergeTI.TiEntityObjects;
using MongoDB;
using TiEntityObject.Transport;
using System.IO;
using TI_EntityLib.TiEntityObjects.Kladr;

namespace TI_Transport
{
    class Program
    {
        static void Main(string[] args)
        {
            //if (args.Length == 0)
            //    DayLoadRun(DateTime.Now);
            //else
            //{
            //    DayLoadRun(Convert.ToDateTime(args[0]));
            //}

           // LoadKladr();
           
        }

        #region Temporary

        static void OrganizationLoad() {
      
            string [] files=Directory.GetFiles (@"C:\TI_DATA\FTP\17.01.2012","*.xml");
            foreach (string i in files){
                LoadOrganizationFile(i);
                Console.WriteLine(i); 
                File.Move (i,i.Replace("xml","OK")); 
            }

            
        }

        /// <summary>
        /// Закачка организаций
        /// </summary>
        /// <param name="fileName"></param>
        static void LoadOrganizationFile(string fileName) {
            XmlDocument d = new XmlDocument();
            d.Load(fileName);

            XmlDocument document = Helper.XmlTools.GetXmlNoNamespaceDocument(d);

            XmlSerializer mySerializer = new XmlSerializer(typeof(masterDataBatch));
            XmlNodeReader r = new XmlNodeReader(document.ChildNodes[1]);
            masterDataBatch entityList = (masterDataBatch)mySerializer.Deserialize(r);

            Mongo mongo = new Mongo();
            IMongoDatabase db = mongo.GetDatabase("TI");
            string script = string.Empty;
            mongo.Connect();
            foreach (DictionaryOrganization i in entityList.OrganizationList)
            {

                script = "mergeOrganization(" + i.ToString() + ");";
                db.Eval(script);
            }
            mongo.Disconnect();
            mongo.Dispose();
        }

        static void LoadKladr() {

            XmlDocument d = new XmlDocument();
            d.Load(@"C:\Projects\PharmTender\Prototype\Kladr\KladrItems_1_2.xml");

            KladrItemsRoot r = new KladrItemsRoot(d.ChildNodes[0]); 
            

            Mongo mongo = new Mongo();
            IMongoDatabase db = mongo.GetDatabase("TI");
            string script = string.Empty;
            mongo.Connect();
            foreach (KladrItem  i in r.Items )
            {
                script = "db.Kladr.insert("+i.ToString ()+")";
                db.Eval(script);
            }
            mongo.Disconnect();
            mongo.Dispose();
        
        }

        static void ClifarDictionaryLoad()
        {
            //XmlDocument d = new XmlDocument();
            //d.Load(@"C:\TEMP\INN.xml");
            //XmlSerializer mySerializer = new XmlSerializer(typeof(dataroot));
            //XmlNodeReader r = new XmlNodeReader(d.ChildNodes[1]);
            //dataroot entityList = (dataroot)mySerializer.Deserialize(r);

            //Mongo mongo = new Mongo();
            //IMongoDatabase db = mongo.GetDatabase("TI");
            //string script = string.Empty;
            //mongo.Connect();
            //foreach (Inn i in entityList.List )
            //{

            //        script = "db.INN.insert("+i.ToString()+");";
            //        db.Eval(script);
            //}    
            //        mongo.Disconnect();
            //        mongo.Dispose();
     }
       
        static void DayLoadRun(DateTime d)
        {          
            DayLoader dl= new DayLoader (d);
            dl.Run();
        
        }
        
        static void FastLoadRun()
        {
            TransportConfig.DefautCreateConfig();
            TransportConfig config = TransportConfig.ReadConfig();
            FastFileLoader.config = config;
            FastFileLoader.TheardCount = 1;
            FastFileLoader.RUN();
        }
        #endregion
    }
}
